package com.smrs.basicdata.webapp.action;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletContext;

import org.apache.struts2.ServletActionContext;

public class UploadFileAction extends BaseBasicDataAction{
		/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

		protected String titleName="渠道";

		private static final int BUFFER_SIZE=16*1024;  
	    // 文件标题  
	    private String title;  
	    // 用File数组来封装多个上传文件域对象  
	    private File[] upload;  
	    // 用String数组来封装多个上传文件名  
	    private String[] uploadFileName;  
	    // 用String数组来封装多个上传文件类型  
	    private String[] uploadContentType;  
	    // 保存文件的目录路径(通过依赖注入)  
	    private String savePath;  
	    //以下为所有属性的getter和setter。省略。。。  
	    // 自己封装的一个把源文件对象复制成目标文件对象  
	    private static boolean  copy(File src, File dst) {  
	        boolean result=false;  
	        InputStream in = null;  
	        OutputStream out = null;  
	        try {  
	            in = new BufferedInputStream(new FileInputStream(src), BUFFER_SIZE);  
	            out = new BufferedOutputStream(new FileOutputStream(dst), BUFFER_SIZE);  
	            byte[] buffer = new byte[BUFFER_SIZE];  
	            int len = 0;  
	            while ((len = in.read(buffer)) > 0) {  
	                out.write(buffer, 0, len);  
	            }  
	            result=true;  
	        } catch (Exception e) {  
	            e.printStackTrace();  
	            result=false;  
	        } finally {  
	            if (null != in) {  
	                try {  
	                    in.close();  
	                } catch (IOException e) {  
	                    e.printStackTrace();  
	                }  
	            }  
	            if (null != out) {  
	                try {  
	                    out.close();  
	                } catch (IOException e) {  
	                    e.printStackTrace();  
	                }  
	            }  
	        }  
	        return result;  
	    }  

	    
	    public String getTitleName() {
			return titleName;
		}
	
	    @Override  
	    public String execute() throws Exception {  
	        File[] srcFiles = this.getUpload();  
	        List<String> successFileList=new ArrayList<String>();  
	        // 处理每个要上传的文件  
	        for (int i = 0; i < srcFiles.length; i++) {  
	            // 根据服务器的文件保存地址和原文件名创建目录文件全路径  
	           
	        	String dstPath = getReallyPath(getSavePath()) + "\\" + this.getUploadFileName()[i];  
	            File dstFile = new File(dstPath);  
	            if(copy(srcFiles[i], dstFile)){  
	                  successFileList.add(getUploadFileName()[i]);  
	            }
	        
	        }  
	        getRequest().setAttribute("successFileList", successFileList);  
	        return SUCCESS;  
	    }  
	      
	    public String getTitle() {  
	        return title;  
	    }  
	    public void setTitle(String title) {  
	        this.title = title;  
	    }  
	    public File[] getUpload() {  
	        return upload;  
	    }  
	    public void setUpload(File[] upload) {  
	        this.upload = upload;  
	    }  
	    public String[] getUploadFileName() {  
	        return uploadFileName;  
	    }  
	    public void setUploadFileName(String[] uploadFileName) {  
	        this.uploadFileName = uploadFileName;  
	    }  
	    public String[] getUploadContentType() {  
	        return uploadContentType;  
	    }  
	    public void setUploadContentType(String[] uploadContentType) {  
	        this.uploadContentType = uploadContentType;  
	    }  
	    public String getSavePath() {  
	        return savePath;  
	    }  
	    public void setSavePath(String savePath) {  
	        this.savePath = savePath;  
	    } 
	    
	    public ServletContext getServletContext(){  
	        return ServletActionContext.getServletContext();  
	    }  
	    
	    public String getReallyPath(String path){  
	        return getServletContext().getRealPath(path);  
	    } 
}
